﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Client.Factories;
using Lidgren.Network;
using Microsoft.Xna.Framework;
using Shared;

namespace Client.Players
{
    class LocalPlayer:Player, IUpdateSender
    {
        public enum MoveDirection
        {
            Forward = 1,
            Backward = -1,
            Left = -2,
            Right = 2,
            None=0
        }

        public LocalPlayer(Game1 game, long sessionID, int id, string imageAssetPath, Vector2 position, float angle, short index, KeyboardControls controls) : base(game, sessionID, id, imageAssetPath, position, angle, 1, index)
        {
            Controls = controls;
           
        }


        private KeyboardControls Controls { get; set; }

        private MoveDirection currentDirection = MoveDirection.None;
        public void Move(MoveDirection direction)
        {
            currentDirection = direction;            
        }
        private bool CheckCollisions()
        {
            Rectangle curRect = new Rectangle((int)Position.X-(Width/2), (int)Position.Y-(Height/2), Width, Height);
            foreach (Rectangle clip in this.Game.ClipMap.Values)
            {
                if (curRect.Intersects(clip))
                {
                    return true;
                }
            }
            return false;
        }
 
        public void Rotate(float angle)
        {
            Angle += angle;
        }

        public override void Update(GameTime gameTime)
        {
            if (InputKeys.Contains(Controls.Forward))
            {
                Move(MoveDirection.Forward);
            }

            if (InputKeys.Contains(Controls.Backward))
            {
                Move(MoveDirection.Backward);
            }

            if (InputKeys.Contains(Controls.RotateLeft))
            {
                Move(MoveDirection.Left);
            }

            if (InputKeys.Contains(Controls.RotateRight))
            {
                Move(MoveDirection.Right);
            }
            if (currentDirection != MoveDirection.None)
            {
                Vector2 tmpPos = Position;
                Vector2 pos = Position;
                switch (currentDirection)
                {
                    case MoveDirection.Forward:
                        pos.Y = pos.Y - Speed;
                        Position = pos;
                        break;
                    case MoveDirection.Backward:
                        pos.Y = pos.Y + Speed;
                        Position = pos;
                        break;
                    case MoveDirection.Left:
                        pos.X = pos.X - Speed;
                        Position = pos;
                        break;
                    case MoveDirection.Right:
                        pos.X = pos.X + Speed;
                        Position = pos;
                        break;
                }
                if (CheckCollisions())
                {
                    Position = tmpPos;
                    currentDirection = MoveDirection.None;
                }
            }
            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }

       
        public void SendUpdates(NetClient client)
        {
            NetOutgoingMessage om = client.CreateMessage();
            om.Write(Helpers.TransferType.PlayerUpdate);
            om.Write(new PlayerTransferableData(SessionID, ID, Index, Position, Angle, IsValid));
            client.SendMessage(om, NetDeliveryMethod.Unreliable);
        }
    }
}
